package gateway.config.security.service.impl;

import common.model.enums.UserDeletedEnum;
import common.model.po.SysUser;
import common.service.CommonSysUserService;
import gateway.model.dto.SecurityUser;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 查询数据库，获取用户信息
 *
 * @author 米泽鹏
 * @since 2021-08-07 9:08 下午
 */
@Service
public class CustomUserDetailsServiceImpl implements UserDetailsService {

	@Resource
	private CommonSysUserService userService;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		SysUser user = userService.lambdaQuery().eq(SysUser::getUsername, username).eq(SysUser::getIsDeleted, UserDeletedEnum.NOT_DELETED.getValue()).one();
		if (user == null) {
			throw new UsernameNotFoundException("用户不存在：" + username);
		}
		SecurityUser securityUser = new SecurityUser();
		securityUser.setUser(user);
		return securityUser;
	}

}
